<?php
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_payment = "SELECT * FROM order_payment WHERE order_id=".$_REQUEST['order_id']."";
$order_payment = mysql_query($query_order_payment, $drycleaners_conn) or die(mysql_error());
$row_order_payment = mysql_fetch_assoc($order_payment);
$totalRows_order_payment = mysql_num_rows($order_payment);
?>
<div class="bill_wrap">
<?php
$colname_order_by_id = "-1";
if (isset($_GET['order_id'])) {
  $colname_order_by_id = $_GET['order_id'];
}
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_by_id = sprintf("SELECT person.first_name, person.last_name, order.id, order.order_no
  FROM    (   drycleaners.customer customer
           INNER JOIN
              drycleaners.person person
           ON (customer.person_id = person.id))
       INNER JOIN
          drycleaners.`order` `order`
       ON (`order`.customer_id = customer.id)
 WHERE order.id = %s", GetSQLValueString($colname_order_by_id, "int"));
$order_by_id = mysql_query($query_order_by_id, $drycleaners_conn) or die(mysql_error());
$row_order_by_id = mysql_fetch_assoc($order_by_id);
$totalRows_order_by_id = mysql_num_rows($order_by_id);

mysql_free_result($order_by_id);
?>


<div class="bill_header">
<strong>Order No:</strong><?php echo $row_order_by_id["order_no"]?><br/>
<strong>Customer Names:</strong><?php echo $row_order_by_id["first_name"]." ".$row_order_by_id["last_name"]?>
<hr/>
</div>
<div style="margin-left:auto; margin-right:auto; width:100%; text-align:center; margin-bottom:10px;"><strong>Items</strong><hr/></div>
<div class="receipt_body">
<?php $total=0;?>

<?php
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_item_by_order_id = sprintf("SELECT material.name as material_name,
       item.name as item_name,
       type_of_service.name as type_of_service_name,
       order_item.color,
	   order_item.id,
	   order_item.material_id,
	   order_item.item_id
  FROM    (   (   drycleaners.order_item order_item
               INNER JOIN
                  drycleaners.type_of_service type_of_service
               ON (order_item.type_of_service_id = type_of_service.id))
           INNER JOIN
              drycleaners.item item
           ON (order_item.item_id = item.id))
       INNER JOIN
          drycleaners.material material
       ON (order_item.material_id = material.id)
	   WHERE
	    order_id =%s", GetSQLValueString($row_order_by_id["id"], "int"));
$order_item_by_order_id = mysql_query($query_order_item_by_order_id, $drycleaners_conn) or die(mysql_error());
$row_order_item_by_order_id = mysql_fetch_assoc($order_item_by_order_id);

do{
	echo $row_order_item_by_order_id['item_name']." 
	".$row_order_item_by_order_id['material_name']."
	".$row_order_item_by_order_id['type_of_service_name'];
	$cost=0;
	
	mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_item_service = "SELECT order_item_service.id, service.name as service_name,order_item_service.service_id
  FROM    drycleaners.order_item_service order_item_service
       INNER JOIN
          drycleaners.service service
       ON (order_item_service.service_id = service.id) WHERE order_item_id='".$row_order_item_by_order_id['id']."'";
	   
$order_item_service = mysql_query($query_order_item_service, $drycleaners_conn) or die(mysql_error());
$row_order_item_service = mysql_fetch_assoc($order_item_service);
	
	echo"<div class='bill_table'>";
	do{
		echo "<div class='bill_row'><div class='bill_service_column'>".$row_order_item_service['service_name']."</div>";
		
		$query_prices="SELECT material_service_cost.price, material_service_cost.express_extra_price
  FROM drycleaners.material_service_cost material_service_cost Where material_id='".$row_order_item_by_order_id['material_id']."' AND item_id='".$row_order_item_by_order_id['item_id']."' AND service_id='".$row_order_item_service['service_id']."'";

  $get_prices=mysql_query($query_prices,$drycleaners_conn) or die(mysql_error());
  $row_get_prices = mysql_fetch_assoc($get_prices);
	   
	   if($row_order_item_by_order_id["name"]='express'){
	   echo "<div class='bill_price_column'>".($row_get_prices['price'] + $row_get_prices['express_extra_price'])."</div>";
	   $service_cost=$row_get_prices['price']+$row_get_prices['express_extra_price'];
	   }
	   else{
		   echo "<div class='bill_price_column'>".$row_get_prices['price']."/div>";
		   $service_cost=$row_get_prices['price'];
		   }
		   echo "</div>";
		   
		   $cost=$cost+$service_cost;
		}while($row_order_item_service=mysql_fetch_assoc($order_item_service));
		echo"</div>";
	$total=$total+$cost;
	}while($row_order_item_by_order_id=mysql_fetch_assoc($order_item_by_order_id));
?>
<hr/>
<div class="bill_footer">
    <div class="bill_table">
        <div class="bill_row">
            <div class="bill_service_column">
            <strong>Total Bill</strong>
            </div>
            <div class="bill_price_column">
            <?php echo $total;?>
            </div>
        </div>
      </div>
      
        <hr/>
      <div class="bill_table"> 
              <div class="bill_row">
            <div class="bill_service_column">
            <strong>Payments</strong>
            </div>
            <div class="bill_price_column">

            </div>
        </div> 
		<?php 
		if($totalRows_order_payment>0){
		$z=0;$total_payment=0; do{?>
         <div class="bill_row">
            <div class="bill_service_column">
            <?php echo date("d-m-y",strtotime($row_order_payment['created_date']));?>
            </div>
            <div class="bill_price_column">
            <?php echo $row_order_payment['amount'];?>
            </div>
        </div>
        <?php
		$total_payment=$total_payment+$row_order_payment['amount'];
		 $z++;}while($row_order_payment=mysql_fetch_assoc($order_payment));
		 }?>
    </div>
   <div class="bill_table"> 
    <div class="bill_row">
            <div class="bill_service_column">
            <strong>Total Payment</strong>
            </div>
            <div class="bill_price_column">
            <?php echo $total_payment;?>
            </div>
   </div>
   </div>
   <hr />
   <div class="bill_table"> 
       <div class="bill_row">
            <div class="bill_service_column">
            <strong>Balance</strong>
            </div>
            <div class="bill_price_column">
            <?php echo $total-$total_payment;?>
            </div>
   </div>
</div>
<?php
mysql_free_result($order_item_by_order_id);

mysql_free_result($order_item_service);
?>
</div>
<?php
mysql_free_result($order_payment);
?>
